home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / fgdemo10.zip / CHAR.C < prev    next >
Text File  |  1991-10-02  |  14KB  |  542 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  char.c -- character display and management routines                 *
  4. *                                                                      *
  5. \**********************************************************************/
  6.  
  7. #include "defs.h"
  8.  
  9. char *font;
  10. char *pfont;
  11. int  ptsize;
  12. int  *spacing;
  13.  
  14. int spacing_08pt[] = {
  15. /* !  "  #  $  %  &  '  (  )  *  +  ,  - */
  16.    3, 4, 7, 7, 8, 8, 4, 5, 5, 9, 7, 4, 6,
  17.  
  18. /* .  /  0  1  2  3  4  5  6  7  8  9  : */
  19.    3, 6, 7, 4, 7, 7, 7, 7, 7, 7, 7, 7, 3,
  20.  
  21. /* ;  <  =  >  ?  @  A  B  C  D  E  F  G */
  22.    4, 6, 7, 6, 7, 9, 7, 7, 7, 7, 7, 7, 7,
  23.  
  24. /* H  I  J  K  L  M  N  O  P  Q  R  S  T */
  25.    7, 5, 7, 7, 7,10, 8, 7, 7, 7, 7, 7, 7,
  26.  
  27. /* U  V  W  X  Y  Z  [  \  ]  ^  _  `  a */
  28.    7, 7,11, 7, 7, 7, 5, 6, 5, 7, 7, 4, 7,
  29.  
  30. /* b  c  d  e  f  g  h  i  j  k  l  m  n */
  31.    7, 7, 7, 7, 7, 7, 7, 3, 6, 6, 3, 9, 7,
  32.  
  33. /* o  p  q  r  s  t  u  v  w  x  y  z  { */
  34.    7, 7, 7, 6, 7, 6, 7, 7,11, 7, 7, 7, 6,
  35.  
  36. /* |  }  ~ */
  37.    3, 6, 7};
  38.  
  39. int spacing_14pt[] = {
  40. /* !  "  #  $  %  &  '  (  )  *  +  ,  - */
  41.    4, 7, 9, 8,13, 9, 4, 5, 5, 8, 8, 4, 6,
  42.  
  43. /* .  /  0  1  2  3  4  5  6  7  8  9  : */
  44.    4, 5, 8, 6, 8, 8, 8, 8, 8, 8, 8, 8, 4,
  45.  
  46. /* ;  <  =  >  ?  @  A  B  C  D  E  F  G */
  47.    4, 8, 8, 8, 8,13,10,10, 9,10, 9, 9,10,
  48.  
  49. /* H  I  J  K  L  M  N  O  P  Q  R  S  T */
  50.   10, 4, 7, 9, 9,12,10,10,10,10,11, 9,10,
  51.  
  52. /* U  V  W  X  Y  Z  [  \  ]  ^  _  `  a */
  53.   10,10,16,10,12,11, 5, 6, 5, 7, 9, 6, 8,
  54.  
  55. /* b  c  d  e  f  g  h  i  j  k  l  m  n */
  56.    8, 8, 8, 8, 5, 8, 8, 4, 4, 8, 4,12, 8,
  57.  
  58. /* o  p  q  r  s  t  u  v  w  x  y  z  { */
  59.    8, 8, 8, 7, 8, 5, 8,10,12,10,10, 8, 6,
  60.  
  61. /* |  }  ~ */
  62.    5, 6,12};
  63.  
  64.  
  65. /**********************************************************************\
  66. *                                                                      *
  67. *  center_pstring -- display proportional characters, centered         *
  68. *                                                                      *
  69. \**********************************************************************/
  70.  
  71. void center_pstring(string,x1,x2,y)
  72. char *string;
  73. int x1, x2, y;
  74. {
  75.    int x;
  76.  
  77.    x = get_center(string,x1,x2);
  78.    put_pstring(string,x,y);
  79. }
  80.  
  81.  
  82. /**********************************************************************\
  83. *                                                                      *
  84. *  center_string -- display fixed-pitch characters, centered           *
  85. *                                                                      *
  86. \**********************************************************************/
  87.  
  88. void center_string(string,x1,x2,y)
  89. char *string;
  90. int x1, x2, y;
  91. {
  92.    register int i;
  93.    register int nchar;
  94.    int index;
  95.    int x;
  96.    char ch;
  97.  
  98.    nchar = strlen(string);
  99.    if (nchar == 0) return;
  100.  
  101.    /* calculate center */
  102.  
  103.    x = ((x1 + x2) / 2) - (nchar * 4);
  104.  
  105.    /* display each character as a bitmap */
  106.  
  107.    for (i = 0; i < nchar; i++)
  108.    {
  109.       ch = string[i] - 33;
  110.       if (ch >= 0)
  111.       {
  112.          index = ch * ptsize;
  113.          fg_move(x,y);
  114.          fg_drawmap(&font[index],1,ptsize);
  115.       }
  116.       x += 8;
  117.    }
  118. }
  119.  
  120. /**********************************************************************\
  121. *                                                                      *
  122. *  erase_char -- erase a single fixed-pitch character                  *
  123. *                                                                      *
  124. \**********************************************************************/
  125.  
  126. void erase_char(x,y)
  127. int x, y;
  128. {
  129.    register int color;
  130.  
  131.    /* save the current color */
  132.  
  133.    color = fg_getcolor();
  134.  
  135.    /* draw a black rectangle over the character */
  136.  
  137.    fg_setcolor(0);
  138.    fg_rect(x,x+7,y+1-ptsize,y);
  139.  
  140.    /* set the color back to what it was */
  141.  
  142.    fg_setcolor(color);
  143. }
  144.  
  145. /**********************************************************************\
  146. *                                                                      *
  147. *  first_nonblank -- find first character that is not a blank          *
  148. *                                                                      *
  149. \**********************************************************************/
  150.  
  151. char first_nonblank(string)
  152. char *string;
  153. {
  154.    register int i;
  155.    char c;
  156.  
  157.    i = 0;
  158.    while (TRUE)
  159.    {
  160.       c = string[i++];
  161.       if (c == 0)
  162.          return(0);
  163.       else if (c > 32)
  164.          return(c);
  165.    }
  166. }
  167.  
  168. /**********************************************************************\
  169. *                                                                      *
  170. *  get_center -- locate center for proportionally-spaced characters    *
  171. *                                                                      *
  172. \**********************************************************************/
  173.  
  174. get_center(string,x1,x2)
  175. char *string;
  176. int x1, x2;
  177. {
  178.    return(((x1 + x2) / 2) - (length_pstring(string)/ 2));
  179. }
  180.  
  181. /**********************************************************************\
  182. *                                                                      *
  183. *  get_font -- get the proportional and fixed-pitch character fonts    *
  184. *                                                                      *
  185. \**********************************************************************/
  186.  
  187. void get_font()
  188. {
  189.    register int i;
  190.    register int index;
  191.    int x, y;
  192.    int yoffset;
  193.    char fontfile[10];
  194.  
  195.    /* allocate dynamic memory for the fonts; also define font file name */
  196.  
  197.    if (mode11 || mode16)
  198.    {
  199.       font  = malloc(1316);
  200.       pfont = malloc(2632);
  201.       strcpy(fontfile,"14pt.fnt");   /* high res modes use 14 pt font */
  202.       spacing = spacing_14pt;
  203.       ptsize  = 14;
  204.       yoffset = 18;
  205.    }
  206.    else
  207.    {
  208.       font  = malloc(752);
  209.       pfont = malloc(1504);
  210.       strcpy(fontfile,"08pt.fnt");  /* low res modes use 8 pt font */
  211.       spacing = spacing_08pt;
  212.       ptsize  = 8;
  213.       yoffset = 90;
  214.    }
  215.  
  216.    /* if the allocate failed or the font file isn't present, go no farther */
  217.  
  218.    if (font  == (char *)NULL) abort_program(0);
  219.    if (pfont == (char *)NULL) abort_program(0);
  220.    if (!exists(fontfile))     abort_program(1);
  221.  
  222.    /* display either the 8pt or 14pt fonts on the hidden page */
  223.  
  224.    fg_setpage(hidden);
  225.    fg_move(0,199);
  226.    fg_dispfile(fontfile,320,1);
  227.  
  228.    /* retrieve the proportionally spaced and fixed-pitch fonts */
  229.  
  230.    fg_setcolor(11);
  231.    index = 0;
  232.  
  233.    for (i = 0; i < 94; i++)
  234.    {
  235.       x = 1 + (i/13) * 16;
  236.       y = yoffset + (i%13) * (ptsize+1);
  237.       fg_move(x,y);
  238.       fg_getmap(&pfont[index*2],2,ptsize);  /* put in bitmap array */
  239.       x = 129 + (i/13) * 9;
  240.       fg_move(x,y);
  241.       fg_getmap(&font[index],1,ptsize);
  242.       index += ptsize;
  243.    }
  244. }
  245.  
  246. /**********************************************************************\
  247. *                                                                      *
  248. *  get_string -- accept and display a fixed-pitch character string     *
  249. *                                                                      *
  250. \**********************************************************************/
  251.  
  252. get_string(string,x,y,max_length,string_type)
  253. char *string;
  254. int x, y, max_length, string_type;
  255. {
  256.    register int i;
  257.    int color, timer;
  258.    int xmax, ymin;
  259.    unsigned char key, aux;
  260.  
  261.    /* clear an area for the input string */
  262.  
  263.    fg_setcolor(0);
  264.    xmax = x + 8*max_length;
  265.    ymin = y - ptsize;
  266.    fg_rect(x-2,xmax+1,ymin,y+1);
  267.  
  268.    i = 0;
  269.    timer = 16;
  270.    color = 15;
  271.    fg_setcolor(15);
  272.  
  273.    while (i < max_length)
  274.    {
  275.  
  276.       /* increment the timer to make the cursor blink */
  277.  
  278.       timer--;
  279.       if (timer == 8)
  280.          color = 0;
  281.       else if (timer == 0)
  282.       {
  283.          timer = 16;
  284.          color = 15;
  285.       }
  286.       put_cursor(x,y,color);
  287.  
  288.       /* pause, then get a keystroke */
  289.  
  290.       fg_waitfor(1);
  291.       fg_intkey(&key,&aux);
  292.  
  293.       /* printable character */
  294.  
  295.       if (isalnum(key) || ((string_type != ALPHANUMERIC) && ispunct(key)))
  296.       {
  297.